<?php
/*
 * 1626cms 共用函數
 * ============================================================================
 * 版權所有: 1626網絡，并保留所有權利。
 * 網站地址: http://www.1626.com；
 * ----------------------------------------------------------------------------
 * 這不是一個自由軟件！您只能在不用于商業目的的前提下對程序代碼進行修改和
 * 使用；不允許對程序代碼以任何形式任何目的的再發布。
 * ============================================================================
 */
if(!defined('IN_QISHI'))
{
	die('Access Denied!');
}
function p($str = '', $is_exit = 1)
{
	echo '<pre>';
	print_r($str);
	if($is_exit)
	{
		exit;
	}

}
/**
 * 遞歸方式的對變量中的特殊字符進行轉義
 *
 * @access  public
 * @param   mix     $value
 *
 * @return  mix
 */

function addslashes_deep($value)
{
	if (empty($value))
	{
		return $value;
	}
	else
	{
		return is_array($value) ? array_map('addslashes_deep', $value) : addslashes($value);
	}
}

/**
 * 將對象成員變量或者數組的特殊字符進行轉義
 *
 * @access   public
 * @param    mix        $obj      對象或者數組
 *
 * @return   mix                  對象或者數組
 */
function addslashes_deep_obj($obj)
{
	if (is_object($obj) == true)
	{
		foreach ($obj AS $key => $val)
		{
			$obj->$key = addslashes_deep($val);
		}
	}
	else
	{
		$obj = addslashes_deep($obj);
	}

	return $obj;
}

/**
 * 遞歸方式的對變量中的特殊字符去除轉義
 *
 * @access  public
 * @param   mix     $value
 * @return  mix
 */
function stripslashes_deep($value)
{
	if (empty($value))
	{
		return $value;
	}
	else
	{
		return is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
	}
}
/**
 * 對 MYSQL LIKE 的內容進行轉義
 *
 * @access      public
 * @param       string      string  內容
 * @return      string
 */
function mysql_like_quote($str)
{
	return strtr($str, array("\\\\" => "\\\\\\\\", '_' => '\_', '%' => '\%', "\'" => "\\\\\'"));
}
/**
 *
 * 為數據表添加前綴
 *
 */

function table($table)
{
	global $pre;
	return  $pre .$table ;
}
/**
 * 系統提示信息
 */
function showmsg($msg_detail, $msg_type = 0, $links = array(), $auto_redirect = true,$seconds=3)
{
	global $smarty;
	if (count($links) == 0)
	{
		$links[0]['text'] = '返回上一頁';
		$links[0]['href'] = 'javascript:history.go(-1)';
	}
	$smarty->assign('ur_here',     '系統提示');
	$smarty->assign('msg_detail',  $msg_detail);
	$smarty->assign('msg_type',    $msg_type);
	$smarty->assign('links',       $links);
	$smarty->assign('default_url', $links[0]['href']);
	$smarty->assign('auto_redirect', $auto_redirect);
	$smarty->assign('seconds', $seconds);
	$smarty->display('showmsg.htm');
	exit;
}
////清除指定id的緩存
function clear_page_cache($page, $cache_id=NULL)
{
	global $smarty;
	if ($pageid)
	{
		$smarty->clear_cache($page,$cache_id);
	}
	else
	{
		$smarty->clear_cache($page);
	}
}
////定時清除緩存
function timing_clear_cache($timing=180)
{
	global $smarty;
	$timing_clear_dir=QISHI_ROOT_PATH."temp/timing_clear.lock";
	if (file_exists($timing_clear_dir))
	{
		if (strtotime("+".$timing." minutes",filemtime($timing_clear_dir))<time())
		{
			$smarty->clear_all_cache();
			$fp = @fopen($timing_clear_dir, 'wb+');
			fwrite($fp, time());
			fclose($fp);
		}
	}
	else
	{
		$smarty->clear_all_cache();
		$fp = @fopen($timing_clear_dir, 'wb+');
		fwrite($fp, time());
		fclose($fp);
	}
}
/**
 *
 * 獲取網站配置信息
 *
 */
function get_cache($cachename){
	global $db;
	$config_arr = array();
	$cache_file_path =QISHI_ROOT_PATH. "data/cache_".$cachename.".php";
	if (file_exists($cache_file_path))
	{
		include_once($cache_file_path);
		return $data;
	}
	else
	{
		$sql = "SELECT * FROM ".table($cachename);
		$arr = $db->getall($sql);
		foreach($arr as $key=> $val)
		{
			$config_arr[$val['name']] = $val['value'];
		}
		write_static_cache($cache_file_path,$config_arr);
		return $config_arr;
	}
}
//寫入緩存文件
function write_static_cache($cache_file_path, $config_arr){
	$content = "<?php\r\n";
	$content .= "\$data = " . var_export($config_arr, true) . ";\r\n";
	$content .= "?>";
	file_put_contents($cache_file_path, $content, LOCK_EX);
}
//過濾關鍵字
function check_word($noword,$content)
{
	if ($noword && $content)
	{
		if(eregi($noword,$content))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}
/**
 *
 * 獲取用戶IP
 *
 */
function getip()
{
	if(getenv('HTTP_CLIENT_IP')){
		$onlineip=getenv('HTTP_CLIENT_IP');
	}else if(getenv('HTTP_X_FORWARDED_FOR')){
		$onlineip=getenv('HTTP_X_FORWARDED_FOR');
	}else if(getenv('REMOTE_ADDR')){
		$onlineip=getenv('REMOTE_ADDR');
	}else{
		$onlineip=$_SERVER['REMOTE_ADDR'];
	}
	return $onlineip;
}
//獲取記錄總數
function get_total($sql){
	global $db;
	if(empty($sql)){
		return false;
	}
	$row = $db->getone($sql);
	return $row['num'];
}
//添加數據
function inserttable($tablename, $insertsqlarr, $returnid=0, $replace = false, $silent=0) {
	global $db;
	$insertkeysql = $insertvaluesql = $comma = '';
	foreach ($insertsqlarr as $insert_key => $insert_value) {
		$insertkeysql .= $comma.'`'.$insert_key.'`';
		$insertvaluesql .= $comma.'\''.$insert_value.'\'';
		$comma = ', ';
	}
	$method = $replace?'REPLACE':'INSERT';
	$state = $db->query($method." INTO $tablename ($insertkeysql) VALUES ($insertvaluesql)", $silent?'SILENT':'');
	if($returnid && !$replace) {
		return $db->insert_id();
	}else {
		return $state;
	}
}
//保存數據
function updatetable($tablename, $setsqlarr, $wheresqlarr, $silent=0) {
	global $db;
	$setsql = $comma = '';
	foreach ($setsqlarr as $set_key => $set_value) {
		if(is_array($set_value)) {
			$setsql .= $comma.'`'.$set_key.'`'.'='.$set_value[0];
		} else {
			$setsql .= $comma.'`'.$set_key.'`'.'=\''.$set_value.'\'';
		}
		$comma = ', ';
	}
	$where = $comma = '';
	if(empty($wheresqlarr)) {
		$where = '1';
	} elseif(is_array($wheresqlarr)) {
		foreach ($wheresqlarr as $key => $value) {
			$where .= $comma.'`'.$key.'`'.'=\''.$value.'\'';
			$comma = ' AND ';
		}
	} else {
		$where = $wheresqlarr;
	}
	return $db->query("UPDATE ".($tablename)." SET ".$setsql." WHERE ".$where, $silent?"SILENT":"");
}
//計算時間差
function sub_day($endday,$staday){
	$value = $endday - $staday;
	if($value < 0)
	{
		return '';
	}
	elseif($value >= 0 && $value < 59)
	{
		return ($value+1)."秒";
	}
	elseif($value >= 60 && $value < 3600)
	{
		$min = intval($value / 60);
		return $min."分鐘";
	}
	elseif($value >=3600 && $value < 86400)
	{
		$h = intval($value / 3600);
		return $h."小時";
	}
	elseif($value >= 86400 && $value < 86400*30)
	{
		$d = intval($value / 86400);
		return $d."天";
	}
	elseif($value >= 86400*30 && $value < 86400*30*12)
	{
		$mon  = intval($value / (86400*30));
		return $mon."個月";
	}
	else{
		$y = intval($value / (86400*30*12));
		return $y."年";
	}
}
//截取字符串
function cut_str($string,$sublen,$start=0,$dot='...',$code='UTF-8')
{
	if($code == 'UTF-8')
	{
		$pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
		preg_match_all($pa, $string, $t_string);
		if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."...";
		return join('', array_slice($t_string[0], $start, $sublen));
	}
	else
	{
		$start = $start*2;
		$sublen = $sublen*2;
		$strlen = strlen($string);
		$tmpstr = '';
		for($i=0; $i< $strlen; $i++)
		{
			if($i>=$start && $i< ($start+$sublen))
			{
				if(ord(substr($string, $i, 1))>129)
				{
					$tmpstr.= substr($string, $i, 2);
				}
				else
				{
					$tmpstr.= substr($string, $i, 1);
				}
			}
			if(ord(substr($string, $i, 1))>129) $i++;
		}
		if(strlen($tmpstr)< $strlen ) $tmpstr.=$dot;
		return $tmpstr;
	}
}
//刪除HTML
function html2text($str){
	$str = preg_replace("/<sty(.*)\\/style>|<scr(.*)\\/script>|<!--(.*)-->/isU","",$str);
	$alltext = "";
	$start = 1;
	for($i=0;$i<strlen($str);$i++)
	{
		if($start==0 && $str[$i]==">")
		{
			$start = 1;
		}
		else if($start==1)
		{
			if($str[$i]=="<")
			{
				$start = 0;
				$alltext .= " ";
			}
			else if(ord($str[$i])>31)
			{
				$alltext .= $str[$i];
			}
		}
	}
	$alltext = str_replace("　"," ",$alltext);
	$alltext = preg_replace("/&([^;&]*)(;|&)/","",$alltext);
	$alltext = preg_replace("/[ ]+/s"," ",$alltext);
	return $alltext;
}
//編譯HTML
function htmldecode($string){
	if(is_array($string)) {
		foreach($string as $key => $val) {
			$string[$key] = htmldecode($val);
		}
	} else {
		$string = preg_replace('/&amp;((#(\d{3,5}|x[a-fA-F0-9]{4})|[a-zA-Z][a-z0-9]{2,5});)/', '&\\1',
		str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $string));
	}
	return $string;
}
//郵件函數
function smtp_mail ($sendto_email,$subject,$body) {
	global $mailconfig,$_CFG;
	$mail = new PHPMailer();
	$mail->IsSMTP();
	$mail->Host = $mailconfig['smtpservers'];//SMTP servers
	$mail->SMTPAuth = true;// 啟用SMTP驗證功能
	$mail->Username = $mailconfig['smtpusername'];
	$mail->Password = $mailconfig['smtppassword'];
	$mail->From =$mailconfig['smtpfrom'];
	$mail->FromName =$_CFG['site_name'];
	$mail->CharSet = "utf-8";
	$mail->Encoding = "base64";
	$mail->AddAddress($sendto_email,"");//收件人地址，可以替換成任何想要接收郵件的email信箱,格式是AddAddress("收件人email","收件人姓名")
	$mail->AddReplyTo($mailconfig['addreplyto'],"");//增加回復標簽addreplyto
	$mail->IsHTML(true);
	$mail->Subject = $subject;
	$mail->Body =$body;
	$mail->AltBody ="text/html";//該屬性的設置是在郵件正文不支持HTML的備用顯示
	if(!$mail->Send())
	{
		return false;
		//echo "郵件發送有誤 <p>";
		//echo "郵件錯誤信息: " . $mail->ErrorInfo;
	}
	else {
		return true;
	}
}
//異步發送mail
function asyn_sendmail($url)
{
	$info = parse_url($url);
	$head = $info['path']."?".$info["query"];
	$fp=fsockopen($info["host"],80,&$errno,&$errstr,30);
	if($fp){
		$get = "GET ".$head." HTTP/1.1\r\n";
		$get .= "Host: ".$info["host"]."\r\n";
		$get .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.5) Gecko/20070713 Firefox/2.0.0.5\r\n";
		$get .= "Referer: http://".$info["host"]."\r\n";
		$get .= "Connection: Close\r\n\r\n";
		fwrite($fp, $get);
		fclose($fp);
	}
}
//---鏈接規則
function url_rewrite($act,$arr=NULL){
	global $_CFG;
	$url = $id0 = $id1 = $id2 = $id3 = $page = '';
	if (is_array($arr)) extract($arr);
	//首頁連接
	if($act=='index')
	{
		if($_CFG['urlrewrite']==1)
		{
			$url =$_CFG['site_dir'].'index';
			$url .= '.html';
			return $url;
		}
		else
		{
			$url =$_CFG['site_dir'].'index.php';
			return $url;
		}
	}
	//會員中心頁面鏈接
	if($act=='user')
	{
		if($_CFG['urlrewrite']==1)
		{
			$url =$_CFG['site_dir'].'user';
			$url .= '.html';
			return $url;
		}
		else
		{
			$url =$_CFG['site_dir'].'user.php';
			return $url;
		}
	}
	//職位列表頁面
	if($act=='jobs')
	{
		if($_CFG['urlrewrite']==1)
		{
			$url =$_CFG['site_dir'].'jobs';
			$url .= '.html';
			return $url;
		}
		else
		{
			$url =$_CFG['site_dir'].'jobs.php';
			return $url;
		}
	}
	//職位鏈接分頁鏈接
	if($act == 'jobs_page')
	{
		if($_CFG['urlrewrite']==1)
		{
			$url =$_CFG['site_dir'].'jobs';
			if ($id0||$id1||$id2||$id3)
			{
				!empty($id0)?$url .= '-'.intval($id0):$url .='-';
				!empty($id1)?$url .= '-'.intval($id1):$url .='-';
				!empty($id2)?$url .= '-'.intval($id2):$url .='-';
				!empty($id3)?$url .= '-'.intval($id3):$url .='-';
			}
			!empty($page)?$url .= '-'.intval($page):$url .='-0';
			$url .= '.html';
			return $url;
		}
		else
		{
			$url =$_CFG['site_dir'].'jobs.php?';
			!empty($id0)?$url .= '&amp;category='.intval($id0):$url .='&amp;category=';
			!empty($id1)?$url .= '&amp;subclass='.intval($id1):$url .='&amp;subclass=';
			!empty($id2)?$url .= '&amp;district='.intval($id2):$url .='&amp;district=';
			!empty($id3)?$url .= '&amp;settr='.intval($id3):$url .='&amp;settr=';
			!empty($page)?$url .= '&amp;page='.intval($page):$url .='&amp;page=0';
			return $url;
		}
	}
	//職位介紹鏈接
	if($act=='jobs-show')
	{
		if($_CFG['urlrewrite']==1)
		{
			$url =$_CFG['site_dir'].'jobs-show';
			$url .= '-'.intval($id0);
			$url .= '.html';
			return $url;
		}
		else
		{
			$url =$_CFG['site_dir'].'jobs-show.php?';
			$url .= 'id='.intval($id0);
			return $url;
		}
	}
	//簡歷列表頁面
	if($act=='resume')
	{
		if($_CFG['urlrewrite']==1)
		{
			$url =$_CFG['site_dir'].'resume';
			$url .= '.html';
			return $url;
		}
		else
		{
			$url =$_CFG['site_dir'].'resume.php';
			return $url;
		}
	}
	//簡歷鏈接分頁鏈接
	if($act == 'resume_page')
	{
		if($_CFG['urlrewrite']==1)
		{
			$url =$_CFG['site_dir'].'resume';
			if ($id0||$id1||$id2||$id3)
			{
				!empty($id0)?$url .= '-'.intval($id0):$url .='-';
				!empty($id1)?$url .= '-'.intval($id1):$url .='-';
				!empty($id2)?$url .= '-'.intval($id2):$url .='-';
				!empty($id3)?$url .= '-'.intval($id3):$url .='-';
			}
			!empty($page)?$url .= '-'.intval($page):$url .='-0';
			$url .= '.html';
			return $url;
		}
		else
		{
			$url =$_CFG['site_dir'].'resume.php?';
			!empty($id0)?$url .= '&amp;category='.intval($id0):$url .='&amp;category=';
			!empty($id1)?$url .= '&amp;subclass='.intval($id1):$url .='&amp;subclass=';
			!empty($id2)?$url .= '&amp;education='.intval($id2):$url .='&amp;education=';
			!empty($id3)?$url .= '&amp;experience='.intval($id3):$url .='&amp;experience=';
			!empty($page)?$url .= '&amp;page='.intval($page):$url .='&amp;page=0';
			return $url;
		}
	}
	//簡歷詳細頁鏈接
	if($act=='resume-show')
	{
		if($_CFG['urlrewrite']==1)
		{
			$url =$_CFG['site_dir'].'resume-show';
			$url .= '-'.intval($id0);
			$url .= '.html';
			return $url;
		}
		else
		{
			$url =$_CFG['site_dir'].'resume-show.php?';
			$url .= 'id='.intval($id0);
			return $url;
		}
	}
	//企業展示鏈接
	if($act=='company')
	{
		if($_CFG['urlrewrite']==1)
		{
			$url =$_CFG['site_dir'].'company';
			$url .= '-'.intval($id0);
			$url .= '.html';
			return $url;
		}
		else
		{
			$url =$_CFG['site_dir'].'company.php?';
			$url .= 'id='.intval($id0);
			return $url;
		}
	}
	//企業電子地圖
	if($act=='company-map')
	{
		if($_CFG['urlrewrite']==1)
		{
			$url =$_CFG['site_dir'].'company-map';
			$url .= '-'.intval($id0);
			$url .= '.html';
			return $url;
		}
		else
		{
			$url =$_CFG['site_dir'].'company-map.php?';
			$url .= 'id='.intval($id0);
			return $url;
		}
	}
	//資訊首頁
	if($act=='news')
	{
		if($_CFG['urlrewrite']==1)
		{
			$url =$_CFG['site_dir'].'news';
			$url .= '.html';
			return $url;
		}
		else
		{
			$url =$_CFG['site_dir'].'news.php';
			return $url;
		}
	}
	//資訊列表
	if($act=='news-list')
	{
		if($_CFG['urlrewrite']==1)
		{
			$url =$_CFG['site_dir'].'news-list';
			$url .= '-'.intval($id0);
			$url .= '.html';
			return $url;
		}
		else
		{
			$url =$_CFG['site_dir'].'news-list.php?';
			$url .= 'id='.intval($id0);
			return $url;
		}
	}
	//資訊詳細頁面
	if($act=='news-show')
	{
		if($_CFG['urlrewrite']==1)
		{
			$url =$_CFG['site_dir'].'news-show';
			$url .= '-'.intval($id0);
			$url .= '.html';
			return $url;
		}
		else
		{
			$url =$_CFG['site_dir'].'news-show.php?';
			$url .= 'id='.intval($id0);
			return $url;
		}
	}
	//資訊列表分頁鏈接
	if($act == 'news_page')
	{
		if($_CFG['urlrewrite']==1)
		{
			$url =$_CFG['site_dir'].'news-list';
			!empty($id0)?$url .= '-'.intval($id0):$url .='-';
			!empty($page)?$url .= '-'.intval($page):$url .='-0';
			$url .= '.html';
			return $url;
		}
		else
		{
			$url =$_CFG['site_dir'].'news-list.php?';
			!empty($id0)?$url .= '&amp;id='.intval($id0):$url .='&amp;id=';
			!empty($page)?$url .= '&amp;page='.intval($page):$url .='&amp;page=0';
			return $url;
		}
	}
	//職位搜索
	if($act=='jobs-search')
	{
		if($_CFG['urlrewrite']==1)
		{
			$url =$_CFG['site_dir'].'jobs-search';
			$url .= '.html';
			return $url;
		}
		else
		{
			$url =$_CFG['site_dir'].'jobs-search.php';
			return $url;
		}
	}
	//職位搜索結果頁
	if($act=='jobs-search-show')
	{
		if($_CFG['urlrewrite']==1)
		{
			$url =$_CFG['site_dir'].'jobs-search-show';
			$url .= '.html';
			return $url;
		}
		else
		{
			$url =$_CFG['site_dir'].'jobs-search-show.php';
			return $url;
		}
	}
	//職位搜索結果頁分頁鏈接
	if($act == 'jobs-search-show-page')
	{
		if($_CFG['urlrewrite']==1)
		{
			$url =$_CFG['site_dir'].'jobs-search-show';
			if ($id0||$id1||$id2||$id3)
			{
				!empty($id0)?$url .= '-'.intval($id0):$url .='-';
				!empty($id1)?$url .= '-'.intval($id1):$url .='-';
				!empty($id2)?$url .= '-'.intval($id2):$url .='-';
				!empty($id3)?$url .= '-'.intval($id3):$url .='-';
			}
			!empty($page)?$url .= '-'.intval($page):$url .='-0';
			$url .= '.html';
			return $url;
		}
		else
		{
			$url =$_CFG['site_dir'].'jobs-search-show.php?';
			!empty($id0)?$url .= '&amp;category='.intval($id0):$url .='&amp;category=';
			!empty($id1)?$url .= '&amp;subclass='.intval($id1):$url .='&amp;subclass=';
			!empty($id2)?$url .= '&amp;district='.intval($id2):$url .='&amp;district=';
			!empty($id3)?$url .= '&amp;settr='.intval($id3):$url .='&amp;settr=';
			!empty($page)?$url .= '&amp;page='.intval($page):$url .='&amp;page=0';
			return $url;
		}
	}
	//簡歷搜索
	if($act=='resume-search')
	{
		if($_CFG['urlrewrite']==1)
		{
			$url =$_CFG['site_dir'].'resume-search';
			$url .= '.html';
			return $url;
		}
		else
		{
			$url =$_CFG['site_dir'].'resume-search.php';
			return $url;
		}
	}
	//簡歷搜索結果頁
	if($act=='resume-search-show')
	{
		if($_CFG['urlrewrite']==1)
		{
			$url =$_CFG['site_dir'].'resume-search-show';
			$url .= '.html';
			return $url;
		}
		else
		{
			$url =$_CFG['site_dir'].'resume-search-show.php';
			return $url;
		}
	}
	//說明頁詳細頁面
	if($act=='explain-show')
	{
		if($_CFG['urlrewrite']==1)
		{
			$url =$_CFG['site_dir'].'explain-show';
			$url .= '-'.intval($id0);
			$url .= '.html';
			return $url;
		}
		else
		{
			$url =$_CFG['site_dir'].'explain-show.php?';
			$url .= 'id='.intval($id0);
			return $url;
		}
	}
	///
}
?>
